package org.eiahe.hr.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eiahe.hr.attendance.domain.SpecialRuleApprovalLog;
import org.eiahe.hr.attendance.domain.bo.SpecialRuleApprovalLogBo;
import org.eiahe.hr.attendance.domain.vo.SpecialRuleApprovalLogVo;
import org.eiahe.hr.attendance.mapper.SpecialRuleApprovalLogMapper;
import org.eiahe.hr.attendance.service.ISpecialRuleApprovalLogService;
import org.ehe.common.core.utils.MapstructUtils;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 特殊规则审批日志Service业务层处理
 *
 * @author zhangnn
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SpecialRuleApprovalLogServiceImpl implements ISpecialRuleApprovalLogService {

    private final SpecialRuleApprovalLogMapper baseMapper;

    @Override
    public SpecialRuleApprovalLogVo queryById(Long logId) {
        return baseMapper.selectVoById(logId);
    }

    @Override
    public TableDataInfo<SpecialRuleApprovalLogVo> queryPageList(SpecialRuleApprovalLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SpecialRuleApprovalLog> lqw = buildQueryWrapper(bo);
        Page<SpecialRuleApprovalLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<SpecialRuleApprovalLogVo> queryList(SpecialRuleApprovalLogBo bo) {
        LambdaQueryWrapper<SpecialRuleApprovalLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SpecialRuleApprovalLog> buildQueryWrapper(SpecialRuleApprovalLogBo bo) {
        LambdaQueryWrapper<SpecialRuleApprovalLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRuleId() != null, SpecialRuleApprovalLog::getRuleId, bo.getRuleId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationType()), SpecialRuleApprovalLog::getOperationType,
                bo.getOperationType());
        lqw.eq(bo.getOperatorId() != null, SpecialRuleApprovalLog::getOperatorId, bo.getOperatorId());
        lqw.orderByDesc(SpecialRuleApprovalLog::getCreateTime);
        return lqw;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(SpecialRuleApprovalLogBo bo) {
        SpecialRuleApprovalLog add = MapstructUtils.convert(bo, SpecialRuleApprovalLog.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLogId(add.getLogId());
        }
        return flag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<SpecialRuleApprovalLogVo> queryByRuleId(Long ruleId) {
        LambdaQueryWrapper<SpecialRuleApprovalLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(SpecialRuleApprovalLog::getRuleId, ruleId);
        lqw.orderByAsc(SpecialRuleApprovalLog::getCreateTime);
        return baseMapper.selectVoList(lqw);
    }
}
